package good.idea.data.excel.write.impl;

import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import good.idea.data.excel.context.ExcelTemplateContext;
import good.idea.data.excel.context.ExcelWriteContext;
import good.idea.data.excel.read.template.ExcelTemplateService;
import good.idea.data.excel.read.template.impl.ExcelTemplateServiceImpl;
import good.idea.data.excel.write.ExcelWriteHelper;
import good.idea.data.excel.write.ExcelWriteService;
import good.idea.data.utils.LangUtils;

import java.io.*;
import java.util.List;

/**
 * @author chm
 */
public class ExcelWriteServiceImpl implements ExcelWriteService {

    @Override
    public void write(ExcelWriteContext writeContext) {
        try {
            ExcelTemplateService templateService = new ExcelTemplateServiceImpl();
            ExcelTemplateContext templateContext = templateService.readTemplate(writeContext.getTemplateName());
            this.executeWrite(writeContext, templateContext);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public File getNewFile(ExcelTemplateContext templateContext) {
        List<File> templateFiles = templateContext.getTemplateFiles();
        if (LangUtils.isNotEmpty(templateFiles.size())) {
            File file = templateFiles.get(0);
            if (file.exists()) {
                return file;
            }
        }
        return null;

    }


    public void executeWrite(ExcelWriteContext writeContext, ExcelTemplateContext templateContext) throws Exception {
        File newFile = this.getNewFile(templateContext);
        InputStream inputStream = new FileInputStream(newFile);
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        SXSSFWorkbook outWorkbook = new SXSSFWorkbook(workbook, 100);
        writeContext.setOutWorkbook(outWorkbook);
        List<XSSFSheet> tempSheets = templateContext.getTempSheets();
        long start = System.currentTimeMillis();
        int tempSize = tempSheets.size();
        for (int i = 0; i < tempSize; i++) {
            templateContext.setActivitySheetIndex(i);
            writeContext.setActivitySheetIndex(i + tempSize);
            ExcelWriteHelper.executeWrite(writeContext, templateContext);
        }
        long end = System.currentTimeMillis();
        System.out.println("数据写入完成:" + (end - start) / 1000.0);
        this.flush(writeContext, tempSize);
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (Exception e) {

        }
    }

    public void flush(ExcelWriteContext writeContext, int tempSize) throws IOException {
        SXSSFWorkbook outWorkbook = writeContext.getOutWorkbook();
        String fileName = System.currentTimeMillis() + "";
        File file = new File("/excel/" + fileName + ".xlsx");
        if (!file.exists()) {
            file.createNewFile();
        }
        OutputStream outputStream = new FileOutputStream(file);
        for (int i = 0; i < tempSize; i++) {
            outWorkbook.removeSheetAt(0);
        }
        outWorkbook.write(outputStream);
    }
}
